home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / jpsrc2.zip / JCHUFF.C < prev    next >
C/C++ Source or Header  |  1991-12-03  |  20KB  |  695 lines

  1. /*
  2.  * jchuff.c
  3.  *
  4.  * Copyright (C) 1991, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains Huffman entropy encoding routines.
  9.  * These routines are invoked via the methods entropy_encode,
  10.  * entropy_encoder_init/term, and entropy_optimize.
  11.  */
  12.  
  13. #include "jinclude.h"
  14.  
  15.  
  16. /* Static variables to avoid passing 'round extra parameters */
  17.  
  18. static compress_info_ptr cinfo;
  19.  
  20. static INT32 huff_put_buffer;    /* current bit-accumulation buffer */
  21. static int huff_put_bits;    /* # of bits now in it */
  22.  
  23. static char * output_buffer;    /* output buffer */
  24. static int bytes_in_buffer;
  25.  
  26.  
  27.  
  28. LOCAL void
  29. fix_huff_tbl (HUFF_TBL * htbl)
  30. /* Compute derived values for a Huffman table */
  31. {
  32.   int p, i, l, lastp, si;
  33.   char huffsize[257];
  34.   UINT16 huffcode[257];
  35.   UINT16 code;
  36.   
  37.   /* Figure 7.3.5.4.2.1: make table of Huffman code length for each symbol */
  38.   /* Note that this is in code-length order. */
  39.  
  40.   p = 0;
  41.   for (l = 1; l <= 16; l++) {
  42.     for (i = 1; i <= (int) htbl->bits[l]; i++)
  43.       huffsize[p++] = (char) l;
  44.   }
  45.   huffsize[p] = 0;
  46.   lastp = p;
  47.   
  48.   /* Figure 7.3.5.4.2.2: generate the codes themselves */
  49.   /* Note that this is in code-length order. */
  50.   
  51.   code = 0;
  52.   si = huffsize[0];
  53.   p = 0;
  54.   while (huffsize[p]) {
  55.     while (((int) huffsize[p]) == si) {
  56.       huffcode[p++] = code;
  57.       code++;
  58.     }
  59.     code <<= 1;
  60.     si++;
  61.   }
  62.   
  63.   /* Figure 7.3.5.4.2.3: generate encoding tables */
  64.   /* These are code and size indexed by symbol value */
  65.  
  66.   for (p = 0; p < lastp; p++) {
  67.     htbl->ehufco[htbl->huffval[p]] = huffcode[p];
  68.     htbl->ehufsi[htbl->huffval[p]] = huffsize[p];
  69.   }
  70.   
  71.   /* Figure 13.4.2.3.1: generate decoding tables */
  72.  
  73.   p = 0;
  74.   for (l = 1; l <= 16; l++) {
  75.     if (htbl->bits[l]) {
  76.       htbl->valptr[l] = p;    /* huffval[] index of 1st sym of code len l */
  77.       htbl->mincode[l] = huffcode[p]; /* minimum code of length l */
  78.       p += htbl->bits[l];
  79.       htbl->maxcode[l] = huffcode[p-1];    /* maximum code of length l */
  80.     } else {
  81.       htbl->maxcode[l] = -1;
  82.     }
  83.   }
  84. }
  85.  
  86.  
  87. /* Outputting bytes to the file */
  88.  
  89. LOCAL void
  90. flush_bytes (void)
  91. {
  92.   if (bytes_in_buffer)
  93.     (*cinfo->methods->entropy_output) (cinfo, output_buffer, bytes_in_buffer);
  94.   bytes_in_buffer = 0;
  95. }
  96.  
  97.  
  98. #define emit_byte(val)  \
  99.   MAKESTMT( if (bytes_in_buffer >= JPEG_BUF_SIZE) \
  100.           flush_bytes(); \
  101.         output_buffer[bytes_in_buffer++] = (char) (val); )
  102.  
  103.  
  104.  
  105. /* Outputting bits to the file */
  106.  
  107. /* Only the right 24 bits of huff_put_buffer are used; the valid bits are
  108.  * left-justified in this part.  At most 16 bits can be passed to emit_bits
  109.  * in one call, and we never retain more than 7 bits in huff_put_buffer
  110.  * between calls, so 24 bits are sufficient.
  111.  */
  112.  
  113. LOCAL void
  114. emit_bits (UINT16 code, int size)
  115. {
  116.   /* This routine is heavily used, so it's worth coding tightly. */
  117.   register INT32 put_buffer = code;
  118.   register int put_bits = huff_put_bits;
  119.  
  120.   put_buffer &= (((INT32) 1) << size) - 1; /* Mask off any excess bits in code */
  121.   
  122.   put_bits += size;        /* new number of bits in buffer */
  123.   
  124.   put_buffer <<= 24 - put_bits; /* align incoming bits */
  125.  
  126.   put_buffer |= huff_put_buffer; /* and merge with old buffer contents */
  127.   
  128.   while (put_bits >= 8) {
  129.     int c = (int) ((put_buffer >> 16) & 0xFF);
  130.     
  131.     emit_byte(c);
  132.     if (c == 0xFF) {        /* need to stuff a zero byte? */
  133.       emit_byte(0);
  134.     }
  135.     put_buffer <<= 8;
  136.     put_bits -= 8;
  137.   }
  138.  
  139.   huff_put_buffer = put_buffer;    /* Update global variables */
  140.   huff_put_bits = put_bits;
  141. }
  142.  
  143.  
  144. LOCAL void
  145. flush_bits (void)
  146. {
  147.   emit_bits((UINT16) 0x7F, 7);    /* fill any partial byte with ones */
  148.   huff_put_buffer = 0;        /* and reset bit-buffer to empty */
  149.   huff_put_bits = 0;
  150. }
  151.  
  152.  
  153.  
  154. /* Encode a single block's worth of coefficients */
  155. /* Note that the DC coefficient has already been converted to a difference */
  156.  
  157. LOCAL void
  158. encode_one_block (JBLOCK block, HUFF_TBL *dctbl, HUFF_TBL *actbl)
  159. {
  160.   register int temp, temp2;
  161.   register int nbits;
  162.   register int k, r, i;
  163.   
  164.   /* Encode the DC coefficient difference per section 7.3.5.1 */
  165.   
  166.   temp = temp2 = block[0];
  167.  
  168.   if (temp < 0) {
  169.     temp = -temp;        /* temp is abs value of input */
  170.     /* For a negative input, want temp2 = bitwise complement of abs(input) */
  171.     /* This code assumes we are on a two's complement machine */
  172.     temp2--;
  173.   }
  174.   
  175.   /* Find the number of bits needed for the magnitude of the coefficient */
  176.   nbits = 0;
  177.   while (temp) {
  178.     nbits++;
  179.     temp >>= 1;
  180.   }
  181.   
  182.   /* Emit the Huffman-coded symbol for the number of bits */
  183.   emit_bits(dctbl->ehufco[nbits], dctbl->ehufsi[nbits]);
  184.  
  185.   /* Emit that number of bits of the value, if positive, */
  186.   /* or the complement of its magnitude, if negative. */
  187.   emit_bits((UINT16) temp2, nbits);
  188.   
  189.   /* Encode the AC coefficients per section 7.3.5.2 */
  190.   
  191.   r = 0;            /* r = run length of zeros */
  192.   
  193.   for (k = 1; k < DCTSIZE2; k++) {
  194.     if ((temp = block[k]) == 0) {
  195.       r++;
  196.     } else {
  197.       /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  198.       while (r > 15) {
  199.     emit_bits(actbl->ehufco[0xF0], actbl->ehufsi[0xF0]);
  200.     r -= 16;
  201.       }
  202.  
  203.       temp2 = temp;
  204.       if (temp < 0) {
  205.     temp = -temp;        /* temp is abs value of input */
  206.     /* This code assumes we are on a two's complement machine */
  207.     temp2--;
  208.       }
  209.       
  210.       /* Find the number of bits needed for the magnitude of the coefficient */
  211.       nbits = 1;        /* there must be at least one 1 bit */
  212.       while (temp >>= 1)
  213.     nbits++;
  214.       
  215.       /* Emit Huffman symbol for run length / number of bits */
  216.       i = (r << 4) + nbits;
  217.       emit_bits(actbl->ehufco[i], actbl->ehufsi[i]);
  218.       
  219.       /* Emit that number of bits of the value, if positive, */
  220.       /* or the complement of its magnitude, if negative. */
  221.       emit_bits((UINT16) temp2, nbits);
  222.       
  223.       r = 0;
  224.     }
  225.   }
  226.  
  227.   /* If the last coef(s) were zero, emit an end-of-block code */
  228.   if (r > 0)
  229.     emit_bits(actbl->ehufco[0], actbl->ehufsi[0]);
  230. }
  231.  
  232.  
  233.  
  234. /*
  235.  * Initialize for a Huffman-compressed scan.
  236.  * This is invoked after writing the SOS marker.
  237.  * The pipeline controller must establish the entropy_output method pointer
  238.  * before calling this routine.
  239.  */
  240.  
  241. METHODDEF void
  242. huff_init (compress_info_ptr xinfo)
  243. {
  244.   short ci;
  245.   jpeg_component_info * compptr;
  246.  
  247.   /* Initialize static variables */
  248.   cinfo = xinfo;
  249.   huff_put_buffer = 0;
  250.   huff_put_bits = 0;
  251.  
  252.   /* Initialize the output buffer */
  253.   output_buffer = (char *) (*cinfo->emethods->alloc_small)
  254.                 ((size_t) JPEG_BUF_SIZE);
  255.   bytes_in_buffer = 0;
  256.  
  257.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  258.     compptr = cinfo->cur_comp_info[ci];
  259.     /* Make sure requested tables are present */
  260.     if (cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no] == NULL ||
  261.     cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no] == NULL)
  262.       ERREXIT(cinfo->emethods, "Use of undefined Huffman table");
  263.     /* Compute derived values for Huffman tables */
  264.     /* We may do this more than once for same table, but it's not a big deal */
  265.     fix_huff_tbl(cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no]);
  266.     fix_huff_tbl(cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no]);
  267.     /* Initialize DC predictions to 0 */
  268.     cinfo->last_dc_val[ci] = 0;
  269.   }
  270.  
  271.   /* Initialize restart stuff */
  272.   cinfo->restarts_to_go = cinfo->restart_interval;
  273.   cinfo->next_restart_num = 0;
  274. }
  275.  
  276.  
  277. /*
  278.  * Emit a restart marker & resynchronize predictions.
  279.  */
  280.  
  281. LOCAL void
  282. emit_restart (compress_info_ptr cinfo)
  283. {
  284.   short ci;
  285.  
  286.   flush_bits();
  287.  
  288.   emit_byte(0xFF);
  289.   emit_byte(RST0 + cinfo->next_restart_num);
  290.  
  291.   /* Re-initialize DC predictions to 0 */
  292.   for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  293.     cinfo->last_dc_val[ci] = 0;
  294.  
  295.   /* Update restart state */
  296.   cinfo->restarts_to_go = cinfo->restart_interval;
  297.   cinfo->next_restart_num++;
  298.   cinfo->next_restart_num &= 7;
  299. }
  300.  
  301.  
  302. /*
  303.  * Encode and output one MCU's worth of Huffman-compressed coefficients.
  304.  */
  305.  
  306. METHODDEF void
  307. huff_encode (compress_info_ptr cinfo, JBLOCK *MCU_data)
  308. {
  309.   short blkn, ci;
  310.   jpeg_component_info * compptr;
  311.   JCOEF temp;
  312.  
  313.   /* Account for restart interval, emit restart marker if needed */
  314.   if (cinfo->restart_interval) {
  315.     if (cinfo->restarts_to_go == 0)
  316.       emit_restart(cinfo);
  317.     cinfo->restarts_to_go--;
  318.   }
  319.  
  320.   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  321.     ci = cinfo->MCU_membership[blkn];
  322.     compptr = cinfo->cur_comp_info[ci];
  323.     /* Convert DC value to difference, update last_dc_val */
  324.     temp = MCU_data[blkn][0];
  325.     MCU_data[blkn][0] -= cinfo->last_dc_val[ci];
  326.     cinfo->last_dc_val[ci] = temp;
  327.     encode_one_block(MCU_data[blkn],
  328.              cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no],
  329.              cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no]);
  330.   }
  331. }
  332.  
  333.  
  334. /*
  335.  * Finish up at the end of a Huffman-compressed scan.
  336.  */
  337.  
  338. METHODDEF void
  339. huff_term (compress_info_ptr cinfo)
  340. {
  341.   /* Flush out the last data */
  342.   flush_bits();
  343.   flush_bytes();
  344.   /* Release the I/O buffer */
  345.   (*cinfo->emethods->free_small) ((void *) output_buffer);
  346. }
  347.  
  348.  
  349.  
  350.  
  351. /*
  352.  * Huffman coding optimization.
  353.  *
  354.  * This actually is optimization, in the sense that we find the best possible
  355.  * Huffman table(s) for the given data.  We first scan the supplied data and
  356.  * count the number of uses of each symbol that is to be Huffman-coded.
  357.  * (This process must agree with the code above.)  Then we build an
  358.  * optimal Huffman coding tree for the observed counts.
  359.  */
  360.  
  361. #ifdef ENTROPY_OPT_SUPPORTED
  362.  
  363.  
  364. /* These are static so htest_one_block can find 'em */
  365. static long * dc_count_ptrs[NUM_HUFF_TBLS];
  366. static long * ac_count_ptrs[NUM_HUFF_TBLS];
  367.  
  368.  
  369. LOCAL void
  370. gen_huff_coding (compress_info_ptr cinfo, HUFF_TBL *htbl, long freq[])
  371. /* Generate the optimal coding for the given counts */
  372. {
  373. #define MAX_CLEN 32        /* assumed maximum initial code length */
  374.   UINT8 bits[MAX_CLEN+1];    /* bits[k] = # of symbols with code length k */
  375.   short codesize[257];        /* codesize[k] = code length of symbol k */
  376.   short others[257];        /* next symbol in current branch of tree */
  377.   int c1, c2;
  378.   int p, i, j;
  379.   long v;
  380.  
  381.   /* This algorithm is explained in section 13.2 of JPEG-8-R8 */
  382.  
  383.   MEMZERO((void *) bits, SIZEOF(bits));
  384.   MEMZERO((void *) codesize, SIZEOF(codesize));
  385.   for (i = 0; i < 257; i++)
  386.     others[i] = -1;        /* init links to empty */
  387.   
  388.   freq[256] = 1;        /* make sure there is a nonzero count */
  389.   /* including the pseudo-symbol 256 in the Huffman procedure guarantees
  390.    * that no real symbol is given code-value of all ones, because 256
  391.    * will be placed in the largest codeword category.
  392.    */
  393.  
  394.   /* Huffman's basic algorithm to assign optimal code lengths to symbols */
  395.  
  396.   for (;;) {
  397.     /* Find the smallest nonzero frequency, set c1 = its symbol */
  398.     /* In case of ties, take the larger symbol number */
  399.     c1 = -1;
  400.     v = 1000000000L;
  401.     for (i = 0; i <= 256; i++) {
  402.       if (freq[i] && freq[i] <= v) {
  403.     v = freq[i];
  404.     c1 = i;
  405.       }
  406.     }
  407.  
  408.     /* Find the next smallest nonzero frequency, set c2 = its symbol */
  409.     /* In case of ties, take the larger symbol number */
  410.     c2 = -1;
  411.     v = 1000000000L;
  412.     for (i = 0; i <= 256; i++) {
  413.       if (freq[i] && freq[i] <= v && i != c1) {
  414.     v = freq[i];
  415.     c2 = i;
  416.       }
  417.     }
  418.  
  419.     /* Done if we've merged everything into one frequency */
  420.     if (c2 < 0)
  421.       break;
  422.     
  423.     /* Else merge the two counts/trees */
  424.     freq[c1] += freq[c2];
  425.     freq[c2] = 0;
  426.  
  427.     /* Increment the codesize of everything in c1's tree branch */
  428.     codesize[c1]++;
  429.     while (others[c1] >= 0) {
  430.       c1 = others[c1];
  431.       codesize[c1]++;
  432.     }
  433.     
  434.     others[c1] = c2;        /* chain c2 onto c1's tree branch */
  435.     
  436.     /* Increment the codesize of everything in c2's tree branch */
  437.     codesize[c2]++;
  438.     while (others[c2] >= 0) {
  439.       c2 = others[c2];
  440.       codesize[c2]++;
  441.     }
  442.   }
  443.  
  444.   /* Now count the number of symbols of each code length */
  445.   for (i = 0; i <= 256; i++) {
  446.     if (codesize[i]) {
  447.       /* The JPEG standard seems to think that this can't happen, */
  448.       /* but I'm paranoid... */
  449.       if (codesize[i] > MAX_CLEN)
  450.     ERREXIT(cinfo->emethods, "Huffman code size table overflow");
  451.  
  452.       bits[codesize[i]]++;
  453.     }
  454.   }
  455.  
  456.   /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
  457.    * Huffman procedure assigned any such lengths, we must adjust the coding.
  458.    * Here is what the JPEG spec says about how this next bit works:
  459.    * Since symbols are paired for the longest Huffman code, the symbols are
  460.    * removed from this length category two at a time.  The prefix for the pair
  461.    * (which is one bit shorter) is allocated to one of the pair; then,
  462.    * skipping the BITS entry for that prefix length, a code word from the next
  463.    * shortest nonzero BITS entry is converted into a prefix for two code words
  464.    * one bit longer.
  465.    */
  466.   
  467.   for (i = MAX_CLEN; i > 16; i--) {
  468.     while (bits[i] > 0) {
  469.       j = i - 2;        /* find length of new prefix to be used */
  470.       while (bits[j] == 0)
  471.     j--;
  472.       
  473.       bits[i] -= 2;        /* remove two symbols */
  474.       bits[i-1]++;        /* one goes in this length */
  475.       bits[j+1] += 2;        /* two new symbols in this length */
  476.       bits[j]--;        /* symbol of this length is now a prefix */
  477.     }
  478.   }
  479.  
  480.   /* Remove the count for the pseudo-symbol 256 from the largest codelength */
  481.   while (bits[i] == 0)        /* find largest codelength still in use */
  482.     i--;
  483.   bits[i]--;
  484.   
  485.   /* Return final symbol counts (only for lengths 0..16) */
  486.   memcpy((void *) htbl->bits, (void *) bits, SIZEOF(htbl->bits));
  487.   
  488.   /* Return a list of the symbols sorted by code length */
  489.   /* It's not real clear to me why we don't need to consider the codelength
  490.    * changes made above, but the JPEG spec seems to think this works.
  491.    */
  492.   p = 0;
  493.   for (i = 1; i <= MAX_CLEN; i++) {
  494.     for (j = 0; j <= 255; j++) {
  495.       if (codesize[j] == i) {
  496.     htbl->huffval[p] = (UINT8) j;
  497.     p++;
  498.       }
  499.     }
  500.   }
  501. }
  502.  
  503.  
  504. /* Process a single block's worth of coefficients */
  505. /* Note that the DC coefficient has already been converted to a difference */
  506.  
  507. LOCAL void
  508. htest_one_block (JBLOCK block, JCOEF block0,
  509.          long dc_counts[], long ac_counts[])
  510. {
  511.   register INT32 temp;
  512.   register int nbits;
  513.   register int k, r;
  514.   
  515.   /* Encode the DC coefficient difference per section 7.3.5.1 */
  516.   
  517.   /* Find the number of bits needed for the magnitude of the coefficient */
  518.   temp = block0;
  519.   if (temp < 0) temp = -temp;
  520.   
  521.   for (nbits = 0; temp; nbits++)
  522.     temp >>= 1;
  523.   
  524.   /* Count the Huffman symbol for the number of bits */
  525.   dc_counts[nbits]++;
  526.   
  527.   /* Encode the AC coefficients per section 7.3.5.2 */
  528.   
  529.   r = 0;            /* r = run length of zeros */
  530.   
  531.   for (k = 1; k < DCTSIZE2; k++) {
  532.     if ((temp = block[k]) == 0) {
  533.       r++;
  534.     } else {
  535.       /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  536.       while (r > 15) {
  537.     ac_counts[0xF0]++;
  538.     r -= 16;
  539.       }
  540.       
  541.       /* Find the number of bits needed for the magnitude of the coefficient */
  542.       if (temp < 0) temp = -temp;
  543.       
  544.       for (nbits = 0; temp; nbits++)
  545.     temp >>= 1;
  546.       
  547.       /* Count Huffman symbol for run length / number of bits */
  548.       ac_counts[(r << 4) + nbits]++;
  549.       
  550.       r = 0;
  551.     }
  552.   }
  553.  
  554.   /* If the last coef(s) were zero, emit an end-of-block code */
  555.   if (r > 0)
  556.     ac_counts[0]++;
  557. }
  558.  
  559.  
  560.  
  561. /*
  562.  * Trial-encode one MCU's worth of Huffman-compressed coefficients.
  563.  */
  564.  
  565. LOCAL void
  566. htest_encode (compress_info_ptr cinfo, JBLOCK *MCU_data)
  567. {
  568.   short blkn, ci;
  569.   jpeg_component_info * compptr;
  570.  
  571.   /* Take care of restart intervals if needed */
  572.   if (cinfo->restart_interval) {
  573.     if (cinfo->restarts_to_go == 0) {
  574.       /* Re-initialize DC predictions to 0 */
  575.       for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  576.     cinfo->last_dc_val[ci] = 0;
  577.       /* Update restart state */
  578.       cinfo->restarts_to_go = cinfo->restart_interval;
  579.     }
  580.     cinfo->restarts_to_go--;
  581.   }
  582.  
  583.   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  584.     ci = cinfo->MCU_membership[blkn];
  585.     compptr = cinfo->cur_comp_info[ci];
  586.     /* NB: unlike the real entropy encoder, we may not change the input data */
  587.     htest_one_block(MCU_data[blkn],
  588.             (JCOEF) (MCU_data[blkn][0] - cinfo->last_dc_val[ci]),
  589.             dc_count_ptrs[compptr->dc_tbl_no],
  590.             ac_count_ptrs[compptr->ac_tbl_no]);
  591.     cinfo->last_dc_val[ci] = MCU_data[blkn][0];
  592.   }
  593. }
  594.  
  595.  
  596.  
  597. /*
  598.  * Find the best coding parameters for a Huffman-coded scan.
  599.  * When called, the scan data has already been converted to a sequence of
  600.  * MCU groups of quantized coefficients, which are stored in a "big" array.
  601.  * The source_method knows how to iterate through that array.
  602.  * On return, the MCU data is unmodified, but the Huffman tables referenced
  603.  * by the scan components may have been altered.
  604.  */
  605.  
  606. METHODDEF void
  607. huff_optimize (compress_info_ptr cinfo, MCU_output_caller_ptr source_method)
  608. /* Optimize Huffman-coding parameters (Huffman symbol table) */
  609. {
  610.   int i, tbl;
  611.   HUFF_TBL **htblptr;
  612.  
  613.   /* Allocate and zero the count tables */
  614.   /* Note that gen_huff_coding expects 257 entries in each table! */
  615.  
  616.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  617.     dc_count_ptrs[i] = NULL;
  618.     ac_count_ptrs[i] = NULL;
  619.   }
  620.  
  621.   for (i = 0; i < cinfo->comps_in_scan; i++) {
  622.     /* Create DC table */
  623.     tbl = cinfo->cur_comp_info[i]->dc_tbl_no;
  624.     if (dc_count_ptrs[tbl] == NULL) {
  625.       dc_count_ptrs[tbl] = (long *) (*cinfo->emethods->alloc_small)
  626.                     (257 * SIZEOF(long));
  627.       MEMZERO((void *) dc_count_ptrs[tbl], 257 * SIZEOF(long));
  628.     }
  629.     /* Create AC table */
  630.     tbl = cinfo->cur_comp_info[i]->ac_tbl_no;
  631.     if (ac_count_ptrs[tbl] == NULL) {
  632.       ac_count_ptrs[tbl] = (long *) (*cinfo->emethods->alloc_small)
  633.                     (257 * SIZEOF(long));
  634.       MEMZERO((void *) ac_count_ptrs[tbl], 257 * SIZEOF(long));
  635.     }
  636.   }
  637.  
  638.   /* Initialize DC predictions to 0 */
  639.   for (i = 0; i < cinfo->comps_in_scan; i++) {
  640.     cinfo->last_dc_val[i] = 0;
  641.   }
  642.   /* Initialize restart stuff */
  643.   cinfo->restarts_to_go = cinfo->restart_interval;
  644.  
  645.   /* Scan the MCU data, count symbol uses */
  646.   (*source_method) (cinfo, htest_encode);
  647.  
  648.   /* Now generate optimal Huffman tables */
  649.   for (tbl = 0; tbl < NUM_HUFF_TBLS; tbl++) {
  650.     if (dc_count_ptrs[tbl] != NULL) {
  651.       htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
  652.       if (*htblptr == NULL)
  653.     *htblptr = (HUFF_TBL *) (*cinfo->emethods->alloc_small) (SIZEOF(HUFF_TBL));
  654.       /* Set sent_table FALSE so updated table will be written to JPEG file. */
  655.       (*htblptr)->sent_table = FALSE;
  656.       /* Compute the optimal Huffman encoding */
  657.       gen_huff_coding(cinfo, *htblptr, dc_count_ptrs[tbl]);
  658.       /* Release the count table */
  659.       (*cinfo->emethods->free_small) ((void *) dc_count_ptrs[tbl]);
  660.     }
  661.     if (ac_count_ptrs[tbl] != NULL) {
  662.       htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
  663.       if (*htblptr == NULL)
  664.     *htblptr = (HUFF_TBL *) (*cinfo->emethods->alloc_small) (SIZEOF(HUFF_TBL));
  665.       /* Set sent_table FALSE so updated table will be written to JPEG file. */
  666.       (*htblptr)->sent_table = FALSE;
  667.       /* Compute the optimal Huffman encoding */
  668.       gen_huff_coding(cinfo, *htblptr, ac_count_ptrs[tbl]);
  669.       /* Release the count table */
  670.       (*cinfo->emethods->free_small) ((void *) ac_count_ptrs[tbl]);
  671.     }
  672.   }
  673. }
  674.  
  675.  
  676. #endif /* ENTROPY_OPT_SUPPORTED */
  677.  
  678.  
  679. /*
  680.  * The method selection routine for Huffman entropy encoding.
  681.  */
  682.  
  683. GLOBAL void
  684. jselchuffman (compress_info_ptr cinfo)
  685. {
  686.   if (! cinfo->arith_code) {
  687.     cinfo->methods->entropy_encoder_init = huff_init;
  688.     cinfo->methods->entropy_encode = huff_encode;
  689.     cinfo->methods->entropy_encoder_term = huff_term;
  690. #ifdef ENTROPY_OPT_SUPPORTED
  691.     cinfo->methods->entropy_optimize = huff_optimize;
  692. #endif
  693.   }
  694. }
  695.